<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="发行说明" tabindex="-1"><a class="header-anchor" href="#发行说明"><span>发行说明</span></a></h2>
<ul>
<li><a href="#versioning-scheme">语义化版本</a></li>
<li><a href="#support-policy">支持策略</a></li>
<li><a href="#laravel-6.0">Laravel 6.0</a></li>
</ul>
<h2 id="语义化版本" tabindex="-1"><a class="header-anchor" href="#语义化版本"><span>语义化版本</span></a></h2>
<p>Laravel 和官方扩展包将正式遵循 <a href="https://semver.org/lang/zh-CN/" target="_blank" rel="noopener noreferrer">语义化版本</a>。每六个月会发布一个大版本（如：6.0、7.0），每周可能会有小版本和补丁版本发布。小版本和补丁 <strong>绝不</strong> 包含破坏性变更。</p>
<p>当你在项目或者扩展包依赖于 Laravel 框架时，因大版本会包含破坏性变更，故必须使用大版本限制，如：<code v-pre>^6.0</code>。即便如此，我们仍然会努力减低你升级时的耗时，使其控制在一天或者更少。</p>
<h2 id="支持策略" tabindex="-1"><a class="header-anchor" href="#支持策略"><span>支持策略</span></a></h2>
<p>对于 <code v-pre>LTS</code> 版本，例如 <code v-pre>Laravel 6.0</code>，将会提供为期两年的错误修复和三年的安全修复。这些版本提供了最长时间的支持和维护。对于一般版本，只会提供六个月的错误修复和一年的安全修复。对于所有其他库，包括 <code v-pre>Lumen</code> ，只有最新版本会得到错误修复。</p>
<table>
<thead>
<tr>
<th>版本</th>
<th>发布时间</th>
<th>Bug 修复截止时间</th>
<th>安全修复截止时间</th>
</tr>
</thead>
<tbody>
<tr>
<td>5.5 (LTS)</td>
<td>2017 年 8 月 30 日</td>
<td>2019 年 8 月 30 日</td>
<td>2020 年 8 月 30 日</td>
</tr>
<tr>
<td>5.6</td>
<td>2018 年 2 月 7 日</td>
<td>2018 年 8 月 7 日</td>
<td>2019 年 2 月 7 日</td>
</tr>
<tr>
<td>5.7</td>
<td>2018 年 9 月 4 日</td>
<td>2019 年 3 月 4 日</td>
<td>2019 年 9 月 4 日</td>
</tr>
<tr>
<td>5.8</td>
<td>2019 年 2 月 26 日</td>
<td>2019 年 8 月 26 日</td>
<td>2020 年 2 月 26 日</td>
</tr>
<tr>
<td>6.0 (LTS)</td>
<td>2019 年 9 月 3 日</td>
<td>2021 年 9 月 3 日</td>
<td>2022 年 9 月 3 日</td>
</tr>
</tbody>
</table>
<h2 id="laravel-6-0" tabindex="-1"><a class="header-anchor" href="#laravel-6-0"><span>Laravel 6.0</span></a></h2>
<p>Laravel 6.0（LTS）通过引入<a href="https://semver.org/lang/zh-CN/" target="_blank" rel="noopener noreferrer">语义化版本</a>、与<a href="https://vapor.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Vapor</a>的兼容性、优化了授权响应、任务中间件、懒集合、子查询优化、并将前端脚手架单独分离到<code v-pre>laravel/ui</code> Composer 软件包，以及其他各种错误修复和可用性改进。</p>
<h3 id="语义版本控制" tabindex="-1"><a class="header-anchor" href="#语义版本控制"><span>语义版本控制</span></a></h3>
<p>laravel 框架扩展包现在开始使用<a href="https://semver.org/lang/zh-CN/" target="_blank" rel="noopener noreferrer">语义化版本</a>。这使得框架与已经遵循此版本控制标准的其他第一方Laravel包保持一致。Laravel发布周期将保持不变。</p>
<h3 id="laravel-vapor-兼容性" tabindex="-1"><a class="header-anchor" href="#laravel-vapor-兼容性"><span>Laravel Vapor 兼容性</span></a></h3>
<p><em>Laravel Vapor由<a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a>开发。</em></p>
<p>Laravel 6.0提供了与<a href="https://vapor.laravel.com/" target="_blank" rel="noopener noreferrer">Laravel Vapor</a>的兼容性，这是一个用于Laravel的自动伸缩无服务器部署平台。vapor抽象了管理aws lambda上的laravel应用程序的复杂性，以及将这些应用程序与sqs队列、数据库、redis集群、网络、 CloudFront CDN等接口的复杂性。</p>
<h3 id="改进的异常-ignition" tabindex="-1"><a class="header-anchor" href="#改进的异常-ignition"><span>改进的异常：Ignition</span></a></h3>
<p>Laravel 自带了 <a href="https://github.com/facade/ignition" target="_blank" rel="noopener noreferrer">Ignition</a>，这是一个由 Freek Van der Herten 与 Marcel Pociot 创建的关于异常详情页面的新的开源项目。相较之前的版本，Ignition 具有许多优势，比如改进的错误页面 Blade 文件与行号处理、对常见问题的运行时解决、代码编辑、异常共享以及改进的用户体验。</p>
<h3 id="改进的授权响应" tabindex="-1"><a class="header-anchor" href="#改进的授权响应"><span>改进的授权响应</span></a></h3>
<p><em>改进的授权响应由 <a href="https://github.com/garygreen" target="_blank" rel="noopener noreferrer">Gary Green</a> 实现。</em></p>
<p>在之前的 Laravel 版本中，很难提取并向最终的用户展示自定义的授权消息，这就导致了很难跟最终用户解释某个特定请求为什么会被拒绝。在 Laravel 6.0 中，使用授权响应消息和新方法 <code v-pre>Gate::inspect</code> 将会让事情变得更加容易。比如，给定以下授权策略（Policy）方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line">* 判断用户能否查看指定的航班</span>
<span class="line">*</span>
<span class="line">* <span class="token keyword">@param</span> <span class="token class-name"><span class="token punctuation">\</span>App<span class="token punctuation">\</span>User</span> <span class="token parameter">$user</span></span>
<span class="line">* <span class="token keyword">@param</span> <span class="token class-name"><span class="token punctuation">\</span>App<span class="token punctuation">\</span>Flight</span> <span class="token parameter">$flight</span></span>
<span class="line">* <span class="token keyword">@return</span>  <span class="token class-name"><span class="token keyword">mixed</span></span></span>
<span class="line">*/</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">view</span><span class="token punctuation">(</span><span class="token class-name type-declaration">User</span>  <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Flight</span>  <span class="token variable">$flight</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">deny</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Explanation of denial.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>Gate::inspect</code> 方法可以很容易地提取出授权策略的响应和消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Gate</span><span class="token operator">::</span><span class="token function">inspect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view'</span><span class="token punctuation">,</span> <span class="token variable">$flight</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">allowed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 用户被允许查看该航班……</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">denied</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span>  <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">message</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除此之外，当你在路由或者控制器中使用了诸如 <code v-pre>$this-&gt;authorize</code> 或 <code v-pre>Gate::authorize</code> 的辅助方法后，这些自定义信息将能够自动地返回给你的前端。</p>
<h3 id="任务中间件" tabindex="-1"><a class="header-anchor" href="#任务中间件"><span>任务中间件</span></a></h3>
<p>_任务中间件是由 <a href="https://github.com/taylorotwell" target="_blank" rel="noopener noreferrer">Taylor Otwell</a> 实现的_。</p>
<p>任务中间件允许你围绕任务队列的执行封装自定义逻辑，从而减少任务本身的引用。例如：在 Laravel 的上一个发行版本里，你可能封装了一个有关速率限制的回调逻辑在任务的 <code v-pre>handle</code> 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行任务</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">throttle</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">block</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">allow</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">every</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'获取锁...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 处理任务...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 无法获取锁...</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">release</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在 Laravel 6.0 里，这个逻辑可以被提取到一个任务中间件中，从而让你任务的 <code v-pre>handle</code> 方法不用承担任何速率限制的职责：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>Middleware</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Redis</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">RateLimited</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理任务队列</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">mixed</span></span>  <span class="token parameter">$job</span></span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name">callable</span>  <span class="token parameter">$next</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">mixed</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token variable">$job</span><span class="token punctuation">,</span> <span class="token variable">$next</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Redis</span><span class="token operator">::</span><span class="token function">throttle</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">block</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">allow</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">every</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$job</span><span class="token punctuation">,</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token comment">// 获取锁...</span></span>
<span class="line"></span>
<span class="line">                    <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$job</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$job</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token comment">// 无法获取锁...</span></span>
<span class="line"></span>
<span class="line">                    <span class="token variable">$job</span><span class="token operator">-></span><span class="token function">release</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>创建中间件之后，它们可以通过返回任务的 <code v-pre>middleware</code> 方法连接到任务：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>Middleware<span class="token punctuation">\</span>RateLimited</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获得任务的中间件</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">middleware</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">RateLimited</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="懒集合" tabindex="-1"><a class="header-anchor" href="#懒集合"><span>懒集合</span></a></h3>
<p>懒集合 由 <a href="https://github.com/JosephSilber" target="_blank" rel="noopener noreferrer">Joseph Silber</a>开发 。</p>
<p>许多开发者已经感受到了 <code v-pre>Laravel</code><a href="https://learnku.com/docs/laravel/6.x/collections" target="_blank" rel="noopener noreferrer">集合</a> 的强大。为了补充已经很强大的 <code v-pre>集合</code> 类， <code v-pre>Laravel 6.0</code> 引入了 一个，利用 <code v-pre>PHP</code> 的 <a href="https://www.php.net/manual/zh/language.generators.overview.php" target="_blank" rel="noopener noreferrer">生成器</a> 来实现的 <code v-pre>懒集合</code>，允许你使用非常大的数据集合的同时保持较低的内存使用率。</p>
<p>例如，想像你的应用程序需要处理 1 个多 GB 大小的日志文件，同时利用 <code v-pre>Laravel</code> 的集合来解析日志。通过懒集合可以在给定时间内仅将文件的一部分保留在内存中，而不是一次将整个文件都读入内存：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>LogEntry</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>LazyCollection</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">LazyCollection</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$handle</span> <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'log.txt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'r'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token variable">$line</span> <span class="token operator">=</span> <span class="token function">fgets</span><span class="token punctuation">(</span><span class="token variable">$handle</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">yield</span> <span class="token variable">$line</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">-></span><span class="token function">chunk</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">-></span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">LogEntry</span><span class="token operator">::</span><span class="token function">fromLines</span><span class="token punctuation">(</span><span class="token variable">$lines</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">-></span><span class="token function">each</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">LogEntry</span> <span class="token variable">$logEntry</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// Process the log entry...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，想像你需要迭代 10,000 个 <code v-pre>Eloquent</code> 模型，使用传统 <code v-pre>Laravel</code> 集合时，所有 10,000 个 <code v-pre>Eloquent</code> 模型必须同时加载到内存中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">></span> <span class="token number">500</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，从 <code v-pre>Laravel 6.0</code> 开始，查询构建器的 <code v-pre>cursor</code> 方法已经更新为返回 <code v-pre>懒集合</code> 实例。虽然你仍然只能运行单个查询，但是一次只会在内存中保留一个 <code v-pre>Eloquent</code> 模型。在整个例子中，直到我们真正迭代完每个用户之后，<code v-pre>filter</code> 回调才会执行，从而大大减少了内存的使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">cursor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">></span> <span class="token number">500</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="eloquent-子查询增强功能" tabindex="-1"><a class="header-anchor" href="#eloquent-子查询增强功能"><span>Eloquent 子查询增强功能</span></a></h3>
<p>_Eloquent 子查询增强功能是由 <a href="https://github.com/reinink" target="_blank" rel="noopener noreferrer">Jonathan Reinink</a> 实现的_。</p>
<p>Laravel 6.0 引入了几个对数据库子查询支持的新增强和改进。例如，假设我们有一个航班 <code v-pre>flights</code> 表和一个飞往目的地的 <code v-pre>destinations</code> 表。<code v-pre>flights</code> 表包含一个<code v-pre>arrived_at</code> 列，表示航班何时到达目的地。</p>
<p>使用 Laravel 6.0 中新的子查询选择功能，我们可以使用一个查询选择所有的 <code v-pre>destinations</code> 和最近到达该目的地的航班的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Destination</span><span class="token operator">::</span><span class="token function">addSelect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'last_flight'</span> <span class="token operator">=></span> <span class="token class-name static-context">Flight</span><span class="token operator">::</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">whereColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'destination_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'destinations.id'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'arrived_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>并且我们可以使用添加到查询生成器的 <code v-pre>orderBy</code> 函数中的新子查询功能，根据最后一次航班到达目的地的时间对所有目的地进行排序。同样，这可以在对数据库执行单个查询时完成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Destination</span><span class="token operator">::</span><span class="token function">orderByDesc</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">Flight</span><span class="token operator">::</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'arrived_at'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">whereColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'destination_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'destinations.id'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'arrived_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'desc'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="laravel-ui" tabindex="-1"><a class="header-anchor" href="#laravel-ui"><span>Laravel UI</span></a></h3>
<p>通常在早期版本的 <code v-pre>Laravel</code> 中提供的前端脚手架已被提取到 <code v-pre>laravel/ui</code> Composer 包中。这将允许 UI 脚手架本身可以独立于主框架进行开发和版本化。经过此次更改，默认框架脚手架中已经不存在 <code v-pre>Bootstrap</code> 或 <code v-pre>Vue</code> 代码，并且还从框架中提取了 <code v-pre>make:auth</code> 命令。</p>
<p>你可以通过安装 <code v-pre>laravel/ui</code> 包并使用 <code v-pre>ui</code> Artisan 命令安装前端脚手架 ，来恢复前期 <code v-pre>Laravel</code> 版本中的传统 <code v-pre>Vue</code> / <code v-pre>Bootstrap</code> 脚手架：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>ui</span>
<span class="line"></span>
<span class="line">php artisan ui vue <span class="token operator">--</span>auth</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/releases/5121" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/rel...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/releases/5121" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/rel...</a></p>
</blockquote>
</div></template>


